package io.renren.modules.job.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
@RabbitListener(queues = "send_message")
public class SendTimeMessage {

    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitHandler
    public void sendTimeMessage(Map<String,String> map) throws IOException {
        HashOperations operations = redisTemplate.opsForHash();
        String phone = map.get("phone");
        String time = map.get("time");
        String id = map.get("id");
        String uid = "zhengyisky2008";
        String key = "zaq12wsxcde34rfvbgt5";
        String url = "http://utf8.api.smschinese.cn/?Uid="+uid+"&Key="+key+"&smsMob="+phone+"&smsText=尊敬的"+phone+"：您的面试时间为："+time+"，请您安排好时间，不要给面试官留下不好的印象，祝您面试顺利！！";
        String redisId = (String)operations.get(phone, time);
        if(redisId!=null&&id.equals(redisId)){
//
        }else{
            CloseableHttpClient httpClient = HttpClients.createDefault();
//                HttpGet httpGet = new HttpGet(url);
//                CloseableHttpResponse execute = httpClient.execute(httpGet);
            System.out.println("尊敬的"+phone+"：您的面试时间为："+time);
            operations.put(phone,time,id);
            redisTemplate.expire(phone,40, TimeUnit.MINUTES);
        }
        System.out.println("phone:"+phone+"--time:"+time+"--id:"+id+"--redis:"+redisId);
    }
}
